﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Gargoyle.Roton
{
    public abstract class Environment
    {
        /// <summary>
        /// The Audio interface used by this Environment.
        /// </summary>
        public Audio Audio;

        /// <summary>
        /// The Cache used by this Environment.
        /// </summary>
        public Cache Cache;

        /// <summary>
        /// The Input interface used by this Environment.
        /// </summary>
        public Input Input;

        /// <summary>
        /// The Video interface used by this Environment.
        /// </summary>
        public Video Video;

        /// <summary>
        /// If true, the Environment is currently running.
        /// </summary>
        public bool Running;

        /// <summary>
        /// Random number generator.
        /// </summary>
        protected Random Random;

        /// <summary>
        /// Seed for the random number generator.
        /// </summary>
        protected int RandomSeed;

        /// <summary>
        /// Tick number of the Environment. This is incremented once per Execute().
        /// </summary>
        protected int Tick;

        /// <summary>
        /// Create a new Environment.
        /// </summary>
        /// <param name="newVideo">Video interface to be used by this Environment.</param>
        /// <param name="newAudio">Audio interface to be used by this Environment.</param>
        /// <param name="newInput">Input interface to be used by this Environment.</param>
        /// <param name="newCache">Cache to be used by this Environment.</param>
        public Environment(Video newVideo, Audio newAudio, Input newInput, Cache newCache)
        {
            Audio = newAudio;
            Cache = newCache;
            Input = newInput;
            Video = newVideo;

            SetRandomSeed((int)(DateTime.Now.Ticks & 0x7FFFFFFF));
            Running = true;
        }

        /// <summary>
        /// Retrieve the current tick number.
        /// </summary>
        /// <returns></returns>
        public int GetCurrentTick()
        {
            return Tick;
        }

        /// <summary>
        /// When overridden, executes one tick.
        /// </summary>
        public virtual void Execute()
        {
            Tick++;
        }

        /// <summary>
        /// When overridden, loads a file to the Environment.
        /// </summary>
        /// <param name="fileName">Name of the file to load.</param>
        public virtual void Load(string fileName)
        {
        }

        /// <summary>
        /// When overridden, returns the name of the Environment.
        /// </summary>
        /// <returns></returns>
        public virtual string Name()
        {
            return "Roton";
        }

        /// <summary>
        /// When overridden, saves the Environment to a file.
        /// </summary>
        /// <param name="fileName">Name of the file to save.</param>
        public virtual void Save(string fileName)
        {
        }

        /// <summary>
        /// When overridden, disables execution of this Environment.
        /// </summary>
        public virtual void Quit()
        {
            Running = false;
        }

        /// <summary>
        /// Set the seed for the random number generator. This also resets the random number generator.
        /// </summary>
        /// <param name="newSeed"></param>
        public void SetRandomSeed(int newSeed)
        {
            RandomSeed = newSeed;
            Random = new Random(RandomSeed);
        }
    }
}
